package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.net.InetAddress;
import java.util.concurrent.ConcurrentHashMap;

import javax.swing.DefaultListModel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import command.LogOut;
import controller.Contact;
import controller.ContactsTable;

@SuppressWarnings("serial")
public class ChatScreen extends JPanel {

	protected ChatList listContacts;
	private DefaultListModel listmodel;
	protected JTabbedPane multichatArea;
	protected JMenuBar menu;
	
	protected ConcurrentHashMap<InetAddress, DiscussionTab> onglets;
	
	public ChatScreen() {
		onglets = new ConcurrentHashMap<InetAddress, DiscussionTab>(5);
		initComponents();
	}
	
	public ChatList getChatList()
	{
		return listContacts;
	}
	
	public void removeDiscussion(InetAddress ip)
	{
		multichatArea.remove(onglets.get(ip));
		onglets.remove(ip);
	}
	
	private void initComponents()
	{	
		menu = new JMenuBar();
		JMenu menucomponent = new JMenu("MENU");
		menucomponent.add( new ChatMenuItem("Disconnect", new LogOut()) );
		//menucomponent.add( new ChatMenuItem("Send a file", new SendFile(this)) );
		menu.add( menucomponent );
		
		// utiliser le datamodel, voir comment l'intégrer au chat
		
		listmodel = new DefaultListModel();
		ContactsTable.setModel(listmodel);
		listContacts = new ChatList(listmodel);
		
		multichatArea = new JTabbedPane();
		multichatArea.addTab("Default", defaultPage());
		
		this.setLayout(new BorderLayout());
		
		JScrollPane listScroller = new JScrollPane(listContacts);
		listScroller.setPreferredSize(new Dimension(150, 400));
		
		this.add(menu, BorderLayout.NORTH);
		this.add(new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, listScroller, multichatArea), BorderLayout.CENTER);
		
	}
	
	private class OpenNewDiscussion implements Runnable {
		Contact c;
		
		public OpenNewDiscussion(Contact c)
		{
			this.c = c;
		}
		
		public void run()
		{
			multichatArea.addTab(c.getUsername() + "@" + c.getIpAddr().getHostAddress(), createPage(c));
		}
	}
	
	public void openNewDiscussion(Contact c)
	{
		if( !onglets.containsKey(c.getIpAddr()) )
			SwingUtilities.invokeLater(new OpenNewDiscussion(c));
	}
	
	public void closeAllDiscussion()
	{
		multichatArea.removeAll();
		multichatArea.addTab("Default", defaultPage());
		onglets.clear();
	}
	
	public void cleanChatScreen()
	{
		closeAllDiscussion();
		listmodel.removeAllElements();
	}
	
	private JPanel defaultPage()
	{
		return new DefaultTab();
	}
	
	private JPanel createPage(Contact c)
	{
		DiscussionTab tab = new DiscussionTab(c, multichatArea);
		
		onglets.put(c.getIpAddr(), tab);
				
		return tab;
	}
	
	public void writeDiscussion(String s, InetAddress ip)
	{
		if( !onglets.containsKey(ip) )
		{
			ContactsTable contacts = ContactsTable.getInstance();
			try
			{
				SwingUtilities.invokeAndWait(new OpenNewDiscussion(contacts.get(ip)));
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		onglets.get(ip).displayMessage(s);
	}
	
	public void notidTransfer(String s, InetAddress ip)
	{
		onglets.get(ip).displayTranferInformation(s);
	}
	
	public void notifDeparture(String s, InetAddress ip)
	{
		if( !onglets.containsKey(ip) )
		{
			ContactsTable contacts = ContactsTable.getInstance();
			try
			{
				SwingUtilities.invokeAndWait(new OpenNewDiscussion(contacts.get(ip)));
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		if( onglets.get(ip).displayCloseWindow(s) )
			multichatArea.remove(onglets.get(ip));
		else
			multichatArea.remove(onglets.get(ip));
		// else we also close the window
		
		onglets.remove(ip);
	}
	
	public boolean askUser(String s, InetAddress ip)
	{
		if( !onglets.containsKey(ip) )
		{
			ContactsTable contacts = ContactsTable.getInstance();
			try
			{
				SwingUtilities.invokeAndWait(new OpenNewDiscussion(contacts.get(ip)));
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		return onglets.get(ip).displayDemand(s);
	}	
}
